package tools

import (
	"crypto/hmac"
	"crypto/md5"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
)

//Md5BB is alisa of md5.Sum
var Md5BB = md5.Sum

//Md5 alisa of Md5BS
var Md5 = Md5BS

//Md5BS is shorthand of fmt.Sprintf("%x", md5.Sum(src))
func Md5BS(src []byte) string {
	return fmt.Sprintf("%x", Md5BB(src))
}

//Md5SS is shorthand of fmt.Sprintf("%x", md5.Sum([]byte(src)))
func Md5SS(src string) string {
	return fmt.Sprintf("%x", Md5BB([]byte(src)))
}

//Sha256 hmac sha256
func Sha256(data []byte, secret []byte) ([]byte, error) {
	m := hmac.New(sha256.New, secret)
	_, err := m.Write(data)
	if err != nil {
		return nil, err
	}
	return m.Sum(nil), nil
}

//Sha256S hmac sha256
func Sha256S(data string, secret string) (string, error) {
	ret, err := Sha256([]byte(data), []byte(secret))
	if err != nil {
		return "", err
	}
	return hex.EncodeToString(ret), nil
}
